package cn.HL.pet.service.impl;

import cn.HL.pet.domain.OrderPetAcquisition;
import cn.HL.pet.domain.Pet;
import cn.HL.pet.domain.SearchMasterMsg;
import cn.HL.pet.dto.OrderDto;
import cn.HL.pet.mapper.OrderPetAcquisitionMapper;
import cn.HL.pet.mapper.PetMapper;
import cn.HL.pet.mapper.SearchMasterMsgMapper;
import cn.HL.pet.service.IOrderPetAcquisitionService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-29
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    /**
     * 商家取消订单
     * @param id
     */
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public void cancel(Long id) {
        //修改订单状态
        OrderPetAcquisition ord = orderPetAcquisitionMapper.loadById(id);
        ord.setState(-1);
        orderPetAcquisitionMapper.update(ord);
        //把寻主消息的shopId设置为空
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(ord.getSearchMasterMsgId());
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
        //3.删除宠物
        petMapper.deleteById(ord.getPetId());
    }

    /**
     * 接收订单
     * @param dto
     */
    @Override
    public void confirm(OrderDto dto) {
    //补充信息了查询出现在的订单
        OrderPetAcquisition ord = orderPetAcquisitionMapper.loadById(dto.getId());
        ord.setState(1);
        ord.setPrice(dto.getMoney());
        ord.setPaytype(dto.getPayType());
        //添加宠物信息
        orderPetAcquisitionMapper.update(ord);
        Pet pet = petMapper.loadById(ord.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);
        //修改状态
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(ord.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }
}
